package com.aaa.tmr.controller;

import com.aaa.tmr.entity.Upload;
import com.aaa.tmr.service.CommonFileService;
import com.aaa.tmr.service.UploadService;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.github.pagehelper.PageInfo;
import com.aaa.tmr.util.Result;
import com.aaa.tmr.util.ReturnStatus;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * (Upload)表控制层
 *
 * @author makejava
 * @since 2021-03-24 11:23:38
 */
@Controller
@RequestMapping("upload")
public class UploadController {
    /**
     * 服务对象
     */
    @Resource
    private UploadService uploadService;

    @Resource
    private CommonFileService commonFileService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ResponseBody
    @RequestMapping("selectOne")
    public Upload selectOne(Integer id) {
        return this.uploadService.queryById(id);
    }

    /**
     * 跳转部门分页列表页面
     *
     * @return
     */
    @RequestMapping("toUpload")
    public String toPage() {
        return "back/upload-page";
    }


    /**
     * 分页查询方法
     *
     * @param page
     * @param limit
     * @param upload
     * @return
     */
    @ResponseBody
    @RequestMapping("queryAll")
    public Result queryAll(Integer page, Integer limit, Upload upload) {
        PageInfo pageInfo = uploadService.queryAll(page, limit, upload);
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                pageInfo.getTotal(), pageInfo.getList());
    }


    /**
     * 部门添加
     *
     * @param upload
     * @return
     */
    @ResponseBody
    @PostMapping("insert")
    public Result insert(Upload upload) {
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                0, uploadService.insert(upload));
    }

    /**
     * 部门更新
     *
     * @param upload
     * @return
     */
    @ResponseBody
    @PutMapping("update")
    public Result update(Upload upload) {
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                0, uploadService.update(upload));
    }

    /**
     * 通过主键删除数据
     *
     * @param uploadId 主键
     * @return 是否成功
     */
    @ResponseBody
    @DeleteMapping("deleteById")
    public Result deleteById(Integer uploadId) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.uploadService.deleteById(uploadId));
    }


    /**
     * 通过主键删除数据
     *
     * @param uploadIds 主键
     * @return 是否成功
     */
    @ResponseBody
    @DeleteMapping("deleteByIds")
    public Result deleteByIds(String uploadIds) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.uploadService.batchDeleteById(uploadIds));
    }
    /**
     * 文件上传
     * @param file
     * @return
     */
    @ResponseBody
    @RequestMapping("uploadHeadPic")
    public Result uploadHeadPic(@RequestParam MultipartFile file){
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.commonFileService.uploadFile(file,"files/upload"));
    }

}
